﻿class TrackFsm:
    def __init__(self):
        self.state = "None"
        self.motor_drive_results = {
            "None": None,#啥都没有
            "Mid": None,#直走
            "TurnLeft": None,#别走了左拐
            "TrendLeft": None,#微微拐到左边
            "TurnRight": None,#别走了右拐
            "TrendRight": None#微微拐到右边
        }
        self.pre_state = "None"
        self.backup_mode = False

    def set_state(self, new_state):
        if new_state in self.motor_drive_results:
            # 屏蔽无效更新
            if new_state != self.state:
                # 前置状态更新
                self.pre_state = self.state
                # 当前状态更新
                self.state = new_state
                print("状态更新→"+new_state)
        else:
            print("无效状态")

    def get_motor_drive_result(self):
        return self.motor_drive_results[self.state]
    
    # 设置不同状态下的驱动表现
    def set_state_drive_callback(self,key,callback):
        
        # 验证key是否存在
        if key not in self.motor_drive_results:
            print("无效状态回调Key")
            return 
        
        cur_call_back = self.motor_drive_results[key]
        # 验证是否存在值
        if cur_call_back != None:
            print("回调函数替换")
        else:
            print("回调函数填充")
        # 实际填充
        self.motor_drive_results[key] = callback    
        
    
    def apply_motor_drive(self):
        
        callback = self.motor_drive_results[self.state]
      
        if self.backup_mode:
            
            # 空状态启用前置状态驱动  备用模式启动
            if  self.state == "None":
                print("使用备用驱动状态"+self.pre_state)
                callback = self.motor_drive_results[self.pre_state]
      
        if  callback is None:
            print("非有效回调函数")
            return 
        
        callback()
        